<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>
    <div class="head-title">组件样式</div>

    <div class="component-style">
      <el-form :model="form">
        <el-form-item label="排列方式：">
          <el-radio-group v-model="showComponentData.layout">
            <!--            <el-radio value="single">单视频</el-radio>-->
            <el-radio value="carousel">轮播图</el-radio>
            <el-radio value="grid-2">一行两个</el-radio>
            <el-radio value="horizontal-scroll">一行三个</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题栏显示：">
          <el-radio-group v-model="showComponentData.show_title">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题名称：" v-if="showComponentData.show_title">
          <el-input v-model="showComponentData.title" />
        </el-form-item>
        <el-form-item label="文字大小：" v-if="showComponentData.show_title">
          <div class="flex">
            <el-slider style="width: 70%" v-model="form.itemStyle.fontSize" :min="12" :max="30" />
            <el-input
              type="number"
              style="width: 30%; margin-left: 20px"
              v-model.number="form.itemStyle.fontSize"
              :min="12"
              :max="30"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="字体颜色：" v-if="showComponentData.show_title">
          <el-color-picker v-model="form.itemStyle.color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.itemStyle.color">
            <template #append>
              <el-button @click="form.itemStyle.color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="查看更多：">
          <el-radio-group v-model="showComponentData.show_more">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字体名称" v-if="showComponentData.show_more">
          <el-input v-model="showComponentData.show_more_text"></el-input>
        </el-form-item>
<!--        <el-form-item label="跳转链接" v-if="showComponentData.show_more">-->
<!--          <el-link type="primary" @click="showLinkDataDialog = true">请选择跳转链接</el-link>-->
<!--        </el-form-item>-->
        <el-form-item label="视频标题显示">
          <el-radio-group v-model="showComponentData.text_layout">
            <el-radio value="none">不显示</el-radio>
            <!--            <el-radio value="inside">视频内部</el-radio>-->
            <el-radio value="outside">视频外部</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>

    <div class="add-area">
      <div class="add-title">视频内容</div>
      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        :filter="'.el-input'"
        :preventOnFilter="false"
        class="content"
        @end="handleUpdateComponent"
      >
        <div
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
          class="carousel-item"
        >
          <div class="video-content">
            <!--            <el-form-item label="课程标题：">-->
            <!--              <el-input v-model="item.name"></el-input>-->
            <!--            </el-form-item>-->
            <el-image v-if="item.image" :src="item.image" class="avatar" fit="cover"></el-image>
            <div class="operation">
              <div class="column">
                <p class="title">课程标题</p>
                <el-input v-model="item.name" :disabled="true"></el-input>
              </div>
              <!--              <div class="column">-->
              <!--                <p class="title">跳转链接</p>-->
              <!--                <div class="link">-->
              <!--                  <el-icon style="color: var(&#45;&#45;el-color-primary)"><Link /></el-icon>-->
              <!--                  <el-link type="primary" @click="handleOpenLinkDialog(index)">{{-->
              <!--                    item.link?.name || '请选择跳转链接'-->
              <!--                  }}</el-link></div-->
              <!--                >-->
              <!--              </div>-->
            </div>
          </div>
          <!--          <div class="video-content">-->
          <!--            <video class="video-thumb" :src="item.fileUrl" />-->
          <!--            <div class="mask">-->
          <!--              <div class="opera">-->
          <!--                <p class="upload-text" @click="changeVideo(index)">更换视频</p>-->
          <!--              </div>-->
          <!--            </div>-->
          <!--          </div>-->
          <!--          <div class="link-area">-->
          <!--            <div>跳转链接:</div>-->
          <!--            <div class="link">-->
          <!--              <el-icon style="color: var(&#45;&#45;el-color-primary)"><Link /></el-icon>-->
          <!--              <el-link type="primary" @click="handleOpenLinkDialog(index)">{{-->
          <!--                item.link?.name || '请选择跳转链接'-->
          <!--              }}</el-link></div-->
          <!--            >-->
          <!--          </div>-->
          <el-icon class="del-icon" :size="16" color="red" @click="handleDel(index)"
            ><CircleCloseFilled
          /></el-icon>
        </div>

        <div>
          <!--          <el-button type="primary" @click="handleAdd" plain :icon="Plus" style="width: 100%"-->
          <!--            >添加视频</el-button-->
          <!--          >-->
          <el-tag type="primary" class="submit-btn" @click="handleAdd">选择视频课程</el-tag>
        </div>
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <video-course-choose-dialog
      v-if="showVideoListDialog"
      :show-video-course-dialog="showVideoListDialog"
      :single="true"
      :single-selected-data="showComponentData.default_sub_list[0]"
      @close-dialog="handleCloseDialog"
    ></video-course-choose-dialog>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import VideoCourseChooseDialog from '@/views/decoration/diyComponents/VideoCourseChooseDialog.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { CircleCloseFilled, Link } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import { getCanSelectQuestion } from '@/api/questionbank'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    itemStyle: {
      fontSize: 15,
      color: '#333',
      fontWeight: 600,
      height: 100
    }
  })

  const showLinkDataDialog = ref(false)
  const selectVideoSingle = ref(false)
  const changeIndex = ref()
  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw.itemStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showVideoListDialog = ref(false)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleAdd() {
    showVideoListDialog.value = true
    selectVideoSingle.value = false
  }

  // function changeVideo(index: number) {
  //   showVideoListDialog.value = true
  //   selectVideoSingle.value = true
  //   changeIndex.value = index
  // }

  function handleDel(index: number) {
    showComponentData.value.default_sub_list.splice(index, 1)

    if (showComponentData.value.default_sub_list.length === 0) {
      showComponentData.value.default_sub_list.push({
        id: -1,
        name: '视频课程',
        image: 'https://image.lutongjiakao.com/system/b556ec7ad9fe478b8a14c20ed6f1e50b.png',
        isDefault: true
      })
    }

    handleUpdateComponent()
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  // function handleOpenLinkDialog(index: number) {
  //   showLinkDataDialog.value = true
  //   changeIndex.value = index
  // }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleCloseDialog(value: any) {
    if (value) {
      value.image = value.img
      showComponentData.value.default_sub_list = [value]
      getSpecialQuestionData(value)
      handleUpdateComponent()
    }

    showVideoListDialog.value = false
  }

  async function getSpecialQuestionData(item: any) {
    const res = await getCanSelectQuestion({
      pageNum: 1,
      pageSize: 1000,
      isGroup: '01',
      groupId: item.id
    })
    if (res.code === '00000') {
      const data = res.data.list
      showComponentData.value.course_detail_list = data
    }
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle.fontSize) {
      form.itemStyle.fontSize = showComponentData.value.itemStyle.fontSize
      form.itemStyle.color = showComponentData.value.itemStyle.color
      form.itemStyle.fontWeight = showComponentData.value.itemStyle.fontWeight
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;

      .add-title {
        font-size: 14px;
        color: black;
        font-weight: bold;
        margin-bottom: 20px;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          position: relative;
          display: flex;
          flex-direction: column;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .video-title {
            margin-bottom: 20px;
          }

          .video-content {
            display: flex;
            align-items: center;
          }

          .avatar {
            width: 70px;
            height: 70px;
            border-radius: 12px;
            display: block;
            margin-right: 20px;
          }

          .operation {
            flex: 1;

            .column {
              display: flex;
              align-items: center;
              margin-bottom: 6px;
              width: 100%;

              .title {
                width: 80px;
                font-size: 14px;
                margin-right: 20px;
              }

              .link {
                width: 100%;
                display: flex;
                align-items: center;
                height: 40px;
                border-radius: 6px;
                background-color: #fff;
                padding-left: 10px;
              }
            }
          }

          .link-area {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
          }

          .link {
            width: 80%;
            display: flex;
            align-items: center;
            height: 40px;
            padding-left: 10px;
            background-color: #fff;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }
        }
      }
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }
  }
</style>
